package me.jiatao.jetpack

import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.runBlocking
import org.junit.Test

import org.junit.Assert.*

/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */

fun currTime() = System.currentTimeMillis()

var start: Long = 0

/**
 * flatMapConcat
 */
//fun main() = runBlocking {
//
//    (1..5).asFlow()
//        .onStart { start = currTime() }
//        .onEach { delay(100) }
//        .flatMapConcat {
//            flow {
//                emit("$it: First")
//                delay(500)
//                emit("$it: Second")
//            }
//        }
//        .collect {
//            println("$it at ${System.currentTimeMillis() - start} ms from start")
//        }
//}

/**
 * flatMapMerge
 */
fun main() = runBlocking {

    (1..5).asFlow()
        .onStart { start = currTime() }
        .onEach { delay(100) }
        .flatMapMerge {
            flow {
                emit("$it: First")
                delay(500)
                emit("$it: Second")
            }
        }
        .collect {
            println("$it at ${System.currentTimeMillis() - start} ms from start")
        }
}


//fun main() = runBlocking {
//
//    val flowA = (1..5).asFlow()
//    val flowB = flowOf("one", "two", "three","four","five")
//
//    flowOf(flowA,flowB)
//        .flattenConcat()
//        .collect{ println(it) }
//}

